package com.weaver.drools.common.utils;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * 
 *  @project: Absu_Blog
 *  @author: zhouxinlei
 *  @time：2018年8月9日
 *  @version：ver 1.1
 */
public class DateUtliToos {
	
	public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM = "yyyy-MM";
    public static final String DD = "dd";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String dateToString(Date date, String dateFormat) {
        String str = null;
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        if (date == null) {
            return "";
        } else {
            str = format.format(date);
            return str;
        }
    }
    public static String getDateStr(long timeStmp) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dateFormat.format(new Date(timeStmp));
	}
    public static String dateStringFromat(Date date, String dateFormat) {
        String str = null;
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        if (date == null) {
            return "";
        } else {
            str = format.format(date);
            return str;
        }
    }
    public static Date stringToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        java.sql.Date date = null;

        try {
            format.parse(str);
        } catch (ParseException arg3) {
            arg3.printStackTrace();
        }

        date = java.sql.Date.valueOf(str);
        return date;
    }
    /**
     *  获取指定日期-星期基础信息
     * @param strDate
     * @return
     */
    public static int getWeekByDateStr(String strDate){
		int year = Integer.parseInt(strDate.substring(0, 4));
		int month = Integer.parseInt(strDate.substring(5, 7));
		int day = Integer.parseInt(strDate.substring(8, 10));
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DAY_OF_MONTH, day);
		int week = 0;
		int weekIndex = c.get(Calendar.DAY_OF_WEEK);
		switch (weekIndex){
		case 1://周日
			week = 7;
			break;
		case 2://周一
			week = 1;
			break;
		case 3://周二
			week = 2;
			break;
		case 4://周三
			week = 3;
			break;
		case 5://周四
			week = 4;
			break;
		case 6://周五
			week = 5;
			break;
		case 7://周六
			week = 6;
			break;
		}
		return week;
	}
	/**
	 * 获取当前日期以及星期基础信息
	 * 
	 * @return YYYY年MM月DD日 星期X
	 * 
	 * @author simon
	 * 
	 * @timer 2018年8月9日
	 */
	public static String getFormatDateByWeek() {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int date = calendar.get(Calendar.DATE);
		int week = calendar.get(Calendar.DAY_OF_WEEK);
		String result = year + "年";
		if (month < 10)
			result += "0" + month + "月";
		else
			result += month + "月";
		if (date < 10)
			result += "0" + date + "日";
		else
			result += date + "日";
		switch (week) {
		case 2: {
			result += " 星期一";
			break;
		}
		case 3: {
			result += " 星期二";
			break;
		}
		case 4: {
			result += " 星期三";
			break;
		}
		case 5: {
			result += " 星期四";
			break;
		}
		case 6: {
			result += " 星期五";
			break;
		}
		case 7: {
			result += " 星期六";
			break;
		}
		case 1: {
			result += " 星期天";
			break;
		}
		}
		return result;
	}
    /**
     * 得到两个时间相差的天数
     * @param startDate
     * @param endDate
     * @return
     * @throws IOException
     */
    public static Long getCalculatedDays(Date startDate,Date endDate) throws IOException {
		Long days = 0L;
		try {
			Long diff = startDate.getTime()-endDate.getTime();
			days = diff/(1000*60*60*24);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return days;
	}
    /**
	 * 将Calendar时间转化为特定格式时间字符串
	 * 
	 * flag = true : yyyy-mm-dd hh24:mi:ss
	 * 
	 * flag = false :yyyy-mm-dd
	 * 
	 * @param Calendar
	 *            calendar = java.util.Calendar时间实例
	 * 
	 * @param Boolean
	 *            flag = 指定结果字符串格式
	 * 
	 * @return 特定格式时间字符串
	 * 
	 * @author simon
	 * 
	 * @timer 2007-05-01
	 */
	public static String getFormatDate(Calendar calendar, Boolean flag) {
		StringBuilder date = new StringBuilder();
		date.append(calendar.get(Calendar.YEAR));
		date.append("-");
		int temp = calendar.get(Calendar.MONTH) + 1;
		if (temp < 10)
			date.append("0" + temp);
		else
			date.append(temp);
		date.append("-");
		temp = calendar.get(Calendar.DATE);
		if (temp < 10)
			date.append("0" + temp);
		else
			date.append(temp);
		// 返回： YYYY-MM-DD
		if (flag == false) {
			return date.toString();
		}
		date.append(" ");
		temp = calendar.get(Calendar.HOUR_OF_DAY);
		if (temp < 10)
			date.append("0" + temp);
		else
			date.append(temp);
		date.append(":");
		temp = calendar.get(Calendar.MINUTE);
		if (temp < 10)
			date.append("0" + temp);
		else
			date.append(temp);
		date.append(":");
		temp = calendar.get(Calendar.SECOND);
		if (temp < 10)
			date.append("0" + temp);
		else
			date.append(temp);
		return date.toString();
	}
}
